package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.dto.UserItem;
import com.cheer.job.ExPresentListener;
import com.cheer.service.AuthService;
import com.cheer.service.ExPresentAudioService;
import com.cheer.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/present/audio")
public class ExPresentAudioController {

    @Autowired
    private ExPresentAudioService exPresentAudioService;

    @Autowired
    private AuthService authService;

    @ApiOperation("参与总人数")
    @GetMapping("/statistics")
    public Result<ExPresentAudioStatisticsVO> statistics(HttpSession session) {
        authService.getUserId(session);
        return Result.success(exPresentAudioService.getStatistics());
    }

    @ApiOperation("精选")
    @GetMapping("/search")
    public Result<ExPresentAudioWithVO> search(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                               @RequestParam(required = false) String name,
                                               HttpSession session) {
        List<ExPresentAudioVO> list = exPresentAudioService.search(pageNum, pageSize, name, authService.getUserId(session));
        ExPresentAudioWithVO vo = new ExPresentAudioWithVO()
                .setAudioList(list)
                .setPlayableList(list.stream()
                        .filter(exPresentAudioVO -> exPresentAudioVO.getAudioUrl() != null)
                        .map(ExPresentAudioVO::getId).collect(Collectors.toList()));
        return Result.success(vo);
    }

    @ApiOperation("精选")
    @GetMapping("/good")
    public Result<ExPresentAudioWithVO> good(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                             HttpSession session) {
        List<ExPresentAudioVO> list = exPresentAudioService.getGoodAudios(pageNum, pageSize, authService.getUserId(session));
        ExPresentAudioWithVO vo = new ExPresentAudioWithVO()
                .setAudioList(list)
                .setPlayableList(list.stream()
                        .filter(exPresentAudioVO -> exPresentAudioVO.getAudioUrl() != null)
                        .map(ExPresentAudioVO::getId).collect(Collectors.toList()));
        return Result.success(vo);
    }

    @ApiOperation("最新")
    @GetMapping("/latest")
    public Result<ExPresentAudioWithVO> latest(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                               HttpSession session) {
        List<ExPresentAudioVO> list = exPresentAudioService.getLatestAudios(pageNum, pageSize, authService.getUserId(session));
        ExPresentAudioWithVO vo = new ExPresentAudioWithVO()
                .setAudioList(list)
                .setPlayableList(list.stream()
                        .filter(exPresentAudioVO -> exPresentAudioVO.getAudioUrl() != null)
                        .map(ExPresentAudioVO::getId).collect(Collectors.toList()));
        return Result.success(vo);
    }

    @ApiOperation("类别")
    @GetMapping("/type")
    public Result<ExPresentAudioWithVO> type(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                             @RequestParam(required = false, defaultValue = "1") Integer type,
                                             HttpSession session) {
        List<ExPresentAudioVO> list = exPresentAudioService.getAudiosByType(pageNum, pageSize, type, authService.getUserId(session));
        ExPresentAudioWithVO vo = new ExPresentAudioWithVO()
                .setAudioList(list)
                .setPlayableList(list.stream()
                        .filter(exPresentAudioVO -> exPresentAudioVO.getAudioUrl() != null)
                        .map(ExPresentAudioVO::getId).collect(Collectors.toList()));
        return Result.success(vo);
    }

    @ApiOperation("播放")
    @GetMapping("/info")
    public Result<ExPresentAudioVO> info(@RequestParam Integer id,
                                         HttpSession session) {
        return Result.success(exPresentAudioService.getInfo(id, authService.getUserId(session)));
    }

//    @ApiOperation("下一首")
//    @GetMapping("/good/next")
//    public Result<ExPresentAudioVO> nextGood(@RequestParam(required = false, defaultValue = "1") Integer id,
//                                             HttpSession session) {
//        return Result.success(exPresentAudioService.getNextGoodAudio(id, authService.getUserId(session)));
//    }
//
//    @ApiOperation("随机一首")
//    @GetMapping("/good/random")
//    public Result<ExPresentAudioVO> randomGood(HttpSession session) {
//        return Result.success(exPresentAudioService.getRandomGoodAudio(authService.getUserId(session)));
//    }
//
//    @ApiOperation("下一首")
//    @GetMapping("/latest/next")
//    public Result<ExPresentAudioVO> nextLatest(@RequestParam(required = false, defaultValue = "1") Integer number,
//                                               HttpSession session) {
//        return Result.success(exPresentAudioService.getNextLatestAudio(number, authService.getUserId(session)));
//    }
//
//    @ApiOperation("随机一首")
//    @GetMapping("/latest/random")
//    public Result<ExPresentAudioVO> randomLatest(HttpSession session) {
//        return Result.success(exPresentAudioService.getRandomLatestAudio(authService.getUserId(session)));
//    }
//
//    @ApiOperation("下一首")
//    @GetMapping("/type/next")
//    public Result<ExPresentAudioVO> nextType(@RequestParam(required = false, defaultValue = "1") Integer number,
//                                             @RequestParam(required = false, defaultValue = "1") Integer type,
//                                             HttpSession session) {
//        return Result.success(exPresentAudioService.getNextTypeAudio(number, type, authService.getUserId(session)));
//    }
//
//    @ApiOperation("随机一首")
//    @GetMapping("/type/random")
//    public Result<ExPresentAudioVO> randomType(@RequestParam(required = false, defaultValue = "1") Integer type,
//                                               HttpSession session) {
//        return Result.success(exPresentAudioService.getRandomTypeAudio(type, authService.getUserId(session)));
//    }

    @ApiOperation("我的")
    @GetMapping("/user")
    public Result<ExPresentUserAudioByMonthWithVO> user(HttpSession session) {
        List<ExPresentUserAudioByMonthVO> list = exPresentAudioService.getUserAudio(authService.getUserId(session));
        ExPresentUserAudioByMonthWithVO vo = new ExPresentUserAudioByMonthWithVO()
                .setAudioList(list)
                .setPlayableList(list.stream()
                        .flatMap(exPresentUserAudioByMonthVO -> exPresentUserAudioByMonthVO.getList().stream())
                        .filter(exPresentUserAudioVO -> exPresentUserAudioVO.getAudioUrl() != null)
                        .map(ExPresentUserAudioVO::getId).collect(Collectors.toList()));
        return Result.success(vo);
    }

    @ApiOperation("我的个人信息")
    @GetMapping("/user/info")
    public Result<UserItem> userInfo(HttpSession session) {
        return Result.success(exPresentAudioService.getUserInfo(authService.getUserId(session)));
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id,
                         HttpSession session) {
        exPresentAudioService.delete(id, authService.getUserId(session));
        return Result.success();
    }

    @ApiOperation("生成作品")
    @GetMapping("/generate")
    public Result<Integer> generate(@RequestParam String serviceId, HttpSession session) {
        Integer id = exPresentAudioService.generate(serviceId, authService.getUserId(session));
        ExPresentListener.putTask(id, serviceId);
        return Result.success(id);
    }

    @ApiOperation("点赞/取消点赞")
    @GetMapping("/vote")
    public Result vote(@RequestParam Integer id, @RequestParam Boolean vote, HttpSession session) {
        exPresentAudioService.vote(id, vote, authService.getUserId(session));
        return Result.success();
    }

}
